﻿namespace Site2AppWP8.Config
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.Serialization;
    using System.Windows.Media;

    [DataContract]
    public class AppBarConfig
    {
        public AppBarConfig()
        {
            this.IsEnabled = false;
            this.Buttons = new List<BarButton>();
        }

        [DataMember(Name = "enabled")]
        public bool IsEnabled { get; set; }

        [DataMember(Name = "buttons")]
        public List<BarButton> Buttons { get; set; }

        [DataMember(Name = "argbForegroundColor")]
        public string ForegroundColorHexaCode { get; set; }

        [DataMember(Name = "argbBackgroundColor")]
        public string BackgroundColorHexaCode { get; set; }

        public Color ForegroundColor
        {
            get
            {
                return GetColorFromHexa(this.ForegroundColorHexaCode);
            }
        }

        public Color BackgroundColor
        {
            get
            {
                return GetColorFromHexa(this.BackgroundColorHexaCode);
            }
        }

        public bool IsForegroundColorDefined
        {
            get
            {
                return !string.IsNullOrWhiteSpace(this.ForegroundColorHexaCode);
            }
        }

        public bool IsBackgroundColorDefined
        {
            get
            {
                return !string.IsNullOrWhiteSpace(this.BackgroundColorHexaCode);
            }
        }

        public void AddButton(string label, string icon, string action)
        {
            this.Buttons.Add(this.BasicBtnSetup(label, icon, action));
        }

        public void AddButton(string label, string icon, string iconUri, string action)
        {
            BarButton nbb = this.BasicBtnSetup(label, icon, action);
            this.Buttons.Add(nbb);
        }

        private static Color GetColorFromHexa(string hexaColor)
        {
            if (hexaColor.Length == 5)
            {
                return Color.FromArgb(
                    Convert.ToByte(string.Concat(hexaColor.Substring(1, 1), hexaColor.Substring(1, 1)), 16),
                    Convert.ToByte(string.Concat(hexaColor.Substring(2, 1), hexaColor.Substring(2, 1)), 16),
                    Convert.ToByte(string.Concat(hexaColor.Substring(3, 1), hexaColor.Substring(3, 1)), 16),
                    Convert.ToByte(string.Concat(hexaColor.Substring(4, 1), hexaColor.Substring(4, 1)), 16));
            }
            else
            {
                return Color.FromArgb(
                    Convert.ToByte(hexaColor.Substring(1, 2), 16),
                    Convert.ToByte(hexaColor.Substring(3, 2), 16),
                    Convert.ToByte(hexaColor.Substring(5, 2), 16),
                    Convert.ToByte(hexaColor.Substring(7, 2), 16));
            }
        }

        private BarButton BasicBtnSetup(string label, string icon, string action)
        {
            BarButton nbb = new BarButton()
            {
                Label = label,
                Icon = icon,
                ActionString = action
            };

            return nbb;
        }
    }
}
